﻿Imports System.Data.OleDb
Public Class AddToGallery
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString & getSuffix(fup_picture.FileName)

        If fup_picture.HasFile Then
            fup_picture.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO DesignersImages (User_fkID, DesignerPictures, Description) VALUES (@f1, @f2, @f3)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DesignerConnectionString").ConnectionString)
        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f3", tb_description.Text)

        cmd.ExecuteNonQuery()

        conn.Close()
        cmd.Dispose()
        conn.Dispose()

    End Sub

    Function getSuffix(ByRef fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class